package week06

import "math"

// 714. 买卖股票的最佳时机含手续费
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/

func maxProfit(prices []int, fee int) int {
	var dp = make([][]int, len(prices)+1)
	for i := range dp {
		dp[i] = []int{math.MinInt32, math.MinInt32}
	}
	dp[0][0] = 0
	for i := 1; i <= len(prices); i++ {
		for j := 0; j <= 1; j++ {
			dp[i][j] = dp[i-1][j]
			if j == 0 {
				dp[i][0] = max(dp[i][0], dp[i-1][1] + prices[i-1])
			} else {
				dp[i][1] = max(dp[i][1], dp[i-1][0] - prices[i-1] - fee)
			}
		}
	}
	return dp[len(prices)][0]
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}